﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Service.Global;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Asset;
//using System.Web.Util;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain.Asset;

namespace Eassets.UI.Asset
{
    public partial class TransactionSummaryForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetOrganizationLevelList();
                GetTransactionCode();
                GetDocumentCode();

                gridViewTransactionSummary.DataSource = null;
                gridViewTransactionSummary.DataBind();
            }

        }

        private void GetOrganizationLevelList()
        {
            OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
            List<OrganizationLevelList> organizationLevelLists = new List<OrganizationLevelList>();

            organizationLevelLists =
                organizationLevelListService.GetAllOrganizationLevelList().Where(x => x.OrganizationLevelMode == "B").
                    ToList();

            dropDownListOrganizationLevelList.DataSource = organizationLevelLists;
            dropDownListOrganizationLevelList.DataValueField = "OrganizationLevelListId";
            dropDownListOrganizationLevelList.DataTextField = "Code";
            dropDownListOrganizationLevelList.DataBind();
        }

        protected void dropDownListOrganizationLevelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropDownListOrganizationLevelList.SelectedValue != "0")
            {
                hiddenFieldOrgLevelListId.Value = dropDownListOrganizationLevelList.SelectedValue;
            }

        }

        private void GetTransactionCode()
        {
            TransactionTypeService transactionTypeService = new TransactionTypeService();
            List<TransactionType> transactionTypeList = new List<TransactionType>();

            transactionTypeList =
                transactionTypeService.GetAllTransactionType().ToList();

            dropDownListTransactionCode.DataSource = transactionTypeList;
            dropDownListTransactionCode.DataValueField = "Code";
            dropDownListTransactionCode.DataTextField = "Code";
            dropDownListTransactionCode.DataBind();
        }

        protected void dropDownListTransactionCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropDownListTransactionCode.SelectedValue != "0")
            {
                hiddenFieldTransactionCode.Value = dropDownListTransactionCode.SelectedValue;
            }

        }

        private void GetDocumentCode()
        {
            WorkFlowDocumentService workFlowDocumentService = new WorkFlowDocumentService();
            List<WorkFlowDocument> workFlowDocumentList = new List<WorkFlowDocument>();

            workFlowDocumentList = workFlowDocumentService.GetAllWorkFlowDocument().ToList();

            dropDownListDocumentCode.DataSource = workFlowDocumentList;
            dropDownListDocumentCode.DataValueField = "Code";
            dropDownListDocumentCode.DataTextField = "Code";
            dropDownListDocumentCode.DataBind();
        }

        protected void dropDownListDocumentCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropDownListDocumentCode.SelectedValue != "0")
            {
                hiddenFieldDocumentCode.Value = dropDownListDocumentCode.SelectedValue;
                DataBIndToGridViewTransactionSummary();
            }

        }

        private void DataBIndToGridViewTransactionSummary()
        {
            TransactionService transactionService = new TransactionService();
            List<Transaction> transactionList = new List<Transaction>();

            transactionList =
                transactionService.GetAllTransaction().Where(
                    x =>
                    x.OrganizationLevelListId == Convert.ToInt64(hiddenFieldOrgLevelListId.Value) &&
                    x.TransactionTypeCode == hiddenFieldTransactionCode.Value &&
                    x.TransactionDocumentCode == hiddenFieldDocumentCode.Value).ToList();

            gridViewTransactionSummary.DataSource = transactionList;
            gridViewTransactionSummary.DataBind();
        }

        protected void buttonApply_Click(object sender, EventArgs e)
        {
            //if (dropDownListDocumentCode.SelectedValue != "0")
            //{
            //    DataBIndToGridViewTransactionSummary();
            //}

        }

        protected void buttonPrint_Click(object sender, EventArgs e)
        {
           //foreach(GridViewRow row in gridViewTransactionSummary.Rows)
           //{
           
            //long tranId = Convert.ToInt64((string)gridViewTransactionSummary.SelectedRow.Cells[1].Text);
            //    string tranNo = (string) row.Cells[2].Text;
            //    DateTime tranDate = Convert.ToDateTime((string) row.Cells[3].Text);

                TransactionService transactionService = new TransactionService();
                Transaction transactionMain = new Transaction();
                transactionMain.TransactionDocumentCode = dropDownListDocumentCode.SelectedItem.Text;
                string documentCode = transactionMain.TransactionDocumentCode;
                

                //Transaction transaction = new Transaction();

                ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW",
                                                    "window.open( 'ReportViewerForm.aspx?TransactionDocumentCode=" +
                                                    documentCode +
                                                    " & mainform=TransactionSummary&reqmode=none&com=Ranasinghe Enterprise & Construction pvt Ltd', null, 'height=400,width=800,status=yes,toolbar=no,menubar=no,location=no' );",
                                                    true);

                Session["TransactionMain"] = transactionMain;
                Session["Print"] = "PrintTranSummary";
            //}

        }
    }
}